﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;
using GPS.GPS_DAL;

namespace GPS.Controllers.GPS_DanhMuc
{
    public class GPS_LuuLuongDaoTaoController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        // 
        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }

        public void initialCategorySearchAction(ViewModelSLLDT_FORM viewModel)
        {
            string DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            ViewBag.checkuser = DVGTVT_MA;
            string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, viewModel.pLLDT_DVGTVT_MA);
            viewModel.pLLDT_DVGTVT_MA = getdvgtvt_Ma;
            ViewBag.lstHangGP = new SelectList(_db.Filter<SHANGGPLX>(x => x.HGPLX_MA != "A1" && x.HGPLX_MA != "A2" && x.HGPLX_MA != "A3" && x.HGPLX_MA != "A4"), "HGPLX_MA", "HGPLX_TEN");
        }
        #region Index
        public ActionResult Index(ViewModelSLLDT_FORM SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            //List<CrudModelSLLDT_FROM> model = GetListData(SearchModel);
            var entities = _db.Filter<SLLDT>(o => (SearchModel.pLLDT_DVGTVT_MA == null || o.LLDT_DVGTVT_MA.StartsWith(SearchModel.pLLDT_DVGTVT_MA))
                                       && (SearchModel.pLLDT_HGPLX_MA == null || o.LLDT_HGPLX_MA.StartsWith(SearchModel.pLLDT_HGPLX_MA))
                                       && (SearchModel.pLLDT_ACTIVE == null || o.LLDT_ACTIVE.StartsWith(SearchModel.pLLDT_ACTIVE)))
                                       .OrderBy(o=>o.LLDT_DVGTVT_MA).ToList();
            ViewBag.TotalRow = entities.Count();
            var model = new List<SLLDT>();
            Int16 icount = 1;
            foreach (var currentEntity in entities)
            {
                currentEntity.STT = icount;
                model.Add(Mapper.Map<SLLDT>(currentEntity));
                icount++;
            }
            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }

        [HttpPost]

        #endregion

        #region create

        public ActionResult Create()
        {
            initialCategoryCreateAction();

            return PartialView("_Create", new CrudModelSLLDT());
            // return View(new CrudModelSLLDT());
        }

        public ActionResult Create1()
        {
            initialCategoryCreateAction();
            var model = new CrudModelSLLDT();
            model.LLDT_NGAYNHAP = DateTime.Now;
            model.LLDT_NGUOINHAP = User.Identity.Name;
            return PartialView("_Create1", model);
            // return View(new CrudModelSLLDT());
        }


        [HttpPost]
        public ActionResult Create1(CrudModelSLLDT model)
        {
            initialCategoryCreateAction();
            if (ModelState.IsValid)
            {
                SLLDT _model = new SLLDT();
                GPS.COMMON.Helpers.CopyObject<SLLDT>(model, ref _model);

                _model.ID = EFSequence.GetNextValue("SEQ_SLLDT_ID");


                var _modelExist = _db.GetOne<SLLDT>(o => o.LLDT_HGPLX_MA == model.LLDT_HGPLX_MA && o.LLDT_DVGTVT_MA==model.LLDT_DVGTVT_MA);
                if (_modelExist == null )
                    _db.Insert<SLLDT>(_model);
                else
                {
                    GPS.COMMON.Helpers.CopyObject<SLLDT>(model, ref _modelExist, new List<string> { "ID" });
                    _db.Update<SLLDT>(_modelExist);
                }


                if (string.IsNullOrWhiteSpace(_model.LLDT_HGPLX_MA))
                {
                    var model_DVGTVT = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == _model.LLDT_DVGTVT_MA);
                    model_DVGTVT.DVGTVT_LLDT = _model.LLDT_LUULUONG;
                    _db.Update<SDONVIGTVT>(model_DVGTVT);
                }

                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryCreateAction()
        {
            ViewBag.lstDVGTVT = new SelectList(_db.Filter<SDONVIGTVT>(x => x.DVGTVT_MAQL == "40" && x.DVGTVT_ACTIVE=="1"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.lstHangGP = new SelectList(_db.Filter<SHANGGPLX>(x => x.HGPLX_MA != "A1" && x.HGPLX_MA != "A2" && x.HGPLX_MA != "A3" && x.HGPLX_MA != "A4" ), "HGPLX_MA", "HGPLX_TEN");
        }

        public string GetValue_Indentity()
        {
            string value = "1";
            if (_db.GetAll<SLLDT>().Count() > 0)
            {
                Int64 valueMax = _db.DbContext.SLLDTs.ToList().Max(e => Convert.ToInt64(e.ID));
                value = (valueMax + 1).ToString();
            }
            return value;
        }

        #endregion

        #region Edit
        public ActionResult Edit(string id)
        {
            decimal _ID_LL = decimal.Parse(id);
            CrudModelSLLDT _model = new CrudModelSLLDT();
            SLLDT model = _db.GetOne<SLLDT>(o => o.ID == _ID_LL);
            GPS.COMMON.Helpers.CopyObject<CrudModelSLLDT>(model, ref _model);
            _model.LLDT_NGAYSUA = DateTime.Now;
            _model.LLDT_NGUOISUA = User.Identity.Name;
            initialCategoryEditAction(_model);
            return PartialView("Edit", _model);
        }
        [HttpPost]
        public ActionResult Edit(CrudModelSLLDT model)
        {

            if (ModelState.IsValid)
            {
                var _model = _db.GetOne<SLLDT>(o => o.ID == model.ID);
                GPS.COMMON.Helpers.CopyObject<SLLDT>(model, ref _model);
                _model.LLDT_NGAYSUA = DateTime.Now;
                _model.LLDT_NGUOISUA = User.Identity.Name;
                //   Singleton<GPS_DanhMucDAO>.Inst.GPS_DANHMUC_SLLDT_UPDATE(model);
                _db.Update<SLLDT>(_model);

                if (string.IsNullOrWhiteSpace(_model.LLDT_HGPLX_MA))
                {
                    var model_DVGTVT = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == _model.LLDT_DVGTVT_MA);
                    model_DVGTVT.DVGTVT_LLDT = _model.LLDT_LUULUONG;
                    _db.Update<SDONVIGTVT>(model_DVGTVT);
                }


                _db.SaveChange();

                string url = Url.Action("Index", "GPS_LuuLuongDaoTao");
                return RedirectToAction("Index"); //Json(new { success = true, url = url });
            }
            else
            {
                initialCategoryEditAction(model);
                return PartialView("Edit", model);

            }
        }
        public void initialCategoryEditAction(CrudModelSLLDT view)
        {
            ViewBag.lstHangGP = new SelectList(_db.Filter<SHANGGPLX>(x => x.HGPLX_MA != "A1" && x.HGPLX_MA != "A2" && x.HGPLX_MA != "A3" && x.HGPLX_MA != "A4"), "HGPLX_MA", "HGPLX_TEN");
            //  view.LLDT_ACTIVE = view.LLDT_ACTIVE == "1" ? "Hiệu lực" : "Không hiệu lực";
            ViewBag.lstDVGTVT = _db.Filter<SDONVIGTVT>(x => x.DVGTVT_MAQL == "40" && x.DVGTVT_MA == view.LLDT_DVGTVT_MA).Select(x => x.DVGTVT_TEN).FirstOrDefault();
        }
        #endregion

        #region Detele
        public ActionResult Delete(decimal id)
        {
            CrudModelSLLDT _model = new CrudModelSLLDT();
            SLLDT model = _db.GetOne<SLLDT>(o => o.ID == id);
            _db.DeleteItem<SLLDT>(model);
            return RedirectToAction("Index");
        }
        #endregion

        #region Detail
        public ActionResult Detail(string id)
        {
            decimal _ID_LL = decimal.Parse(id);
            CrudModelSLLDT _model = new CrudModelSLLDT();
            SLLDT model = _db.GetOne<SLLDT>(o => o.ID == _ID_LL);
            GPS.COMMON.Helpers.CopyObject<CrudModelSLLDT>(model, ref _model);
            initialCategoryDetailAction(_model);
            return PartialView("Detail", _model);
        }

        public void initialCategoryDetailAction(CrudModelSLLDT view)
        {
            view.LLDT_ACTIVE = view.LLDT_ACTIVE == "1" ? "Hiệu lực" : "Không hiệu lực";
            ViewBag.lstDVGTVT = _db.Filter<SDONVIGTVT>(x => x.DVGTVT_MAQL == "40" && x.DVGTVT_MA == view.LLDT_DVGTVT_MA).Select(x => x.DVGTVT_TEN).FirstOrDefault();
            ViewBag.lstHangGP = _db.Filter<SHANGGPLX>(x => x.HGPLX_MA == view.LLDT_HGPLX_MA).Select(x => x.HGPLX_TEN).FirstOrDefault();
        }
        #endregion

        public List<CrudModelSLLDT_FROM> GetListData(ViewModelSLLDT_FORM SearchModel)
        {
            var _pID = new OracleParameter("pID", OracleDbType.Decimal, (SearchModel.pID == null ? DBNull.Value : (object)SearchModel.pID), ParameterDirection.Input);
            var _pLLDT_DVGTVT_MA = new OracleParameter("pLLDT_DVGTVT_MA", OracleDbType.Varchar2, (SearchModel.pLLDT_DVGTVT_MA == null ? DBNull.Value : (object)SearchModel.pLLDT_DVGTVT_MA), ParameterDirection.Input);
            var _pLLDT_HGPLX_MA = new OracleParameter("pLLDT_HGPLX_MA", OracleDbType.Varchar2, (SearchModel.pLLDT_HGPLX_MA == null ? DBNull.Value : (object)SearchModel.pLLDT_HGPLX_MA), ParameterDirection.Input);
            var _pLLDT_ACTIVE = new OracleParameter("pLLDT_HGPLX_MA", OracleDbType.Varchar2, (SearchModel.pLLDT_ACTIVE == null ? DBNull.Value : (object)SearchModel.pLLDT_ACTIVE), ParameterDirection.Input);
            var _pRET = new OracleParameter("pRET", OracleDbType.RefCursor, ParameterDirection.Output);
            var entities = _db.DbContext.Database.SqlQuery<CrudModelSLLDT_FROM>("BEGIN GPS_DAOTAO.GPS_DAOTAO_GET_LUULUONGDAOTAO(:pID,:pDVGTVT_MA,:pLLDT_HGPLX_MA, :pLLDT_ACTIVE,:pRET); end;"
                , _pID
                , _pLLDT_DVGTVT_MA
                , _pLLDT_HGPLX_MA
                , _pLLDT_ACTIVE
                , _pRET).ToList();
            return entities;
        }
    }
}